package com.mooc.swxy.tool;

import com.alibaba.fastjson.JSON;
import com.mooc.swxy.pojo.UserInfo;
import com.mooc.swxy.system.ReqURL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CookieValue;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Siaze
 * @date 2022/4/19
 */
public class UserTool {

    public static Map<String,Integer> signs = new HashMap<>();

    static {
        signs.put("shaozeyu318",99999);
        signs.put("nishishabi",99);
        signs.put("666",5);
    }

    synchronized public static boolean startTask(String sign){
        if (signs.containsKey(sign) && signs.get(sign) > 0){
            signs.put(sign,signs.get(sign)-1);
            return true;
        }
        return false;
    }

    private static final Logger logger = LoggerFactory.getLogger(UserTool.class);

    public static UserInfo getInfo(@CookieValue("token") String token){
        Map<String, String> params = new HashMap<>(1);
        params.put("token", token);
        String s = new RequestTool().postRequest(ReqURL.BASE_URL.concat(ReqURL.USER_INFO_URL), MediaType.MULTIPART_FORM_DATA, params, String.class);
        logger.info(s);
        return JSON.parseObject(s, UserInfo.class);
    }
}
